package com.xiaolin.admin.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * @author xiaolin
 * @description 配置Mybatis拦截器
 * @since 2024/5/23
 */
@Configuration
public class MybatisInterceptorConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactories;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) {
            // 添加数据拦截器
            sqlSessionFactory.getConfiguration().addInterceptor(new DataScopePlugin());
        }
    }
}
